package com.joyintech.lianxi;

import cn.hutool.core.util.CharUtil;
import com.joyintech.vo.RequestHeader;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;

public class MacTest {
    public static void main(String[] args) {
        // 服务流水号 19位
        String serviceSn = "1234567890123456789";

        // 服务时间 yyyyMMddHHmmss 14位
        String serviceTime = "20250423101010";

        RequestHeader requestHeader = new RequestHeader();
        requestHeader.setServiceSn("123");
        requestHeader.setServiceId("asdfkjl");

        // mac域 19+14 = 33
        StringBuilder macdomain = new StringBuilder(serviceSn);
        macdomain.append(serviceTime);

        Object object = requestHeader;

        Field[] fields  = object.getClass().getDeclaredFields();

        // 加密长度限制
        int limit = 512;
        for (Field field: fields) {
            field.setAccessible(true);

            Object value = null;
            try {
                value = field.get(object);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (ObjectUtils.isEmpty(value)) {
                continue;
            }

            String result = String.valueOf(value);


            if (StringUtils.isBlank(result)) {
                continue;
            }

            // 只处理可见ASCII字符，可见字符位于32~126之间
            char[] chars = result.toCharArray();
            StringBuilder appendStr = new StringBuilder();
            for (char ch: chars) {
                if (CharUtil.isAsciiPrintable(ch)) {
                    appendStr.append(CharUtil.toString(ch));
                }
            }
            macdomain.append(appendStr);
            if (macdomain.length()>=limit) {
                break;
            }
        }

        // 需要加密处理的字符串
        String macString = null;
        // 长度大于限制截位处理
        if (macdomain.length()>limit) {
            macString = macdomain.substring(0, limit);
        } else {
            macString = macdomain.toString();
        }
        System.out.println("mac->"+macString);

    }
}
